#include "Date.h"
void Date::Print() const{
  const char* weekdays[7] = {"Mon.","Tues.","Wed.","Thrus.","Fri.","Sat.","Sun."};
  Date start;
  int difday = *this-start;
  int weekday = difday%7;
  cout << _year << "/" << _month << "/" << _day << "/" << weekdays[weekday] << endl;
}
bool Date::operator==(const Date &d) const{
    return _year == d._year 
      && _month == d._month 
      && _day == d._day;
  }

bool Date::operator!=(const Date &d) const{
    return !(*this==d);
  }

bool Date::operator>(const Date &d) const{
  if(_year == d._year){
    if(_month == d._month){
      if(_day == d._day){
        return false;
      }
      else{
        return _day > d._day;
      }
    }
    else{
      return _month > d._month;
    }
  }
  else{
    return _year > d._year;
  }
}

bool Date::operator>=(const Date &d) const{
  return (*this > d) || (*this == d);
}

bool Date::operator<(const Date &d) const{
  return !(*this >= d);
}

bool Date::operator<=(const Date &d) const{
  return !(*this > d);
}

Date& Date::operator+=(int day){
  if(day < 0)
  {
    return *this -= -day;
  }
  _day += day;
  while(_day>=GetMonthDay(_year,_month)){
    _day-=GetMonthDay(_year, _month);
    _month++;
    if(_month>12)
    {
      _year++;
      _month = 1;
    }
  }
  return *this;
}

Date Date::operator+(int day) const{
  Date d = *this;
  return d+=day;
}

Date& Date::operator-=(int day){
  if(day < 0)
  {
    return *this += -day;
  }
  _day-=day;
  while(_day < 1)
  {
    _month--;
    if(_month<1)
    {
      _year--;
      _month = 12;
    }
    _day += GetMonthDay(_year, _month);
  }
  return *this;
}

Date Date::operator-(int day) const{
  Date d = *this;
  return d-=day;
}

Date& Date::operator++(){
  return *this+=1;
}

Date Date::operator++(int){
  Date d = *this;
  *this+=1;
  return d;
}

Date& Date::operator--(){
  return *this-=1;
}

Date Date::operator--(int){
  Date d = *this;
  *this-=1;
  return d;
}

//以公元1年1月1日为基准，算出两个日期经历的天数，再相减；
//int Date::operator-(Date d2) const{
//  Date d1 = *this;
//  int day1 = d1._day;
//  int day2 = d2._day;
//  while(d1._month>1)
//  {
//    day1 += GetMonthDay(d1._year, --d1._month);
//  }
//  while(d2._month>1)
//  {
//    day2 += GetMonthDay(d2._year, --d2._month);
//  }
//  while(d1._year>1)
//  {
//    day1 += GetYearDay(--d1._year);
//  }
//  while(d2._year>1)
//  {
//    day2 += GetYearDay(--d2._year);
//  }
//  return day1-day2;
//}

//先求月日差，再求年差
int Date::operator-(const Date &d) const{
  Date max = *this;
  Date min = d;
  int flag = 1;
  if(max < min)
  {
    max = d;
    min = *this;
    flag = -1;
  }
  int difday = max._day-min._day;
  while(max._month>1)
  {
    difday += GetMonthDay(max._year, --max._month);
  }
  while(min._month>1)
  {
    difday -= GetMonthDay(min._year, --min._month);
  }
  while(max._year>min._year)
  {
    difday += GetYearDay(--max._year);
  }
  //while(min._year>1)
  //{
  //  difday -= GetYearDay(--min._year);
  //}
  return difday*flag;
}

//从小日期一天一天往上加，直到两日期相等。
//int Date::operator-(const Date &d){
//  Date max = *this;
//  Date min = d;
//  int flag = 1;
//
//  if(max < min)
//  {
//    max = d;
//    min = *this;
//    flag = -1;
//  }
//
//  int difday = 0;
//  while(max != min)
//  {
//    ++min;
//    ++difday;
//  }
//  return difday*flag;
//  
//}

